package com.liu.lab06.servlet;

import com.liu.lab06.pojo.Music;
import com.liu.lab06.utils.MybatsUtils;
import org.apache.ibatis.session.SqlSession;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
// 刘小文  -2420710103---
@WebServlet(name="UpdateMusicServlet",value = "/updateMusic")
public class UpdateMusicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //1.获取参数
        int id = Integer.parseInt(request.getParameter("id"));
        String musicname = request.getParameter("musicname");
        String singer = request.getParameter("singer");
        String rank = request.getParameter("ranking");
        int category_id=Integer.parseInt(request.getParameter("category_id"));
      //2. 将参数封装在Music的实例中
        Music m = new Music();
        m.setId(id);
        m.setMusicname(musicname);
        m.setSinger(singer);
        m.setRanking(Integer.parseInt(rank));
        m.setCategory_id(category_id);
          //3. 使用MyBatis执行修改操作，并提交事务，关闭SqlSession（补充代码）
        int rows = MybatsUtils.update("MusicMapper.updateMusic", m);
        if (rows>0){
            System.out.println("update success!!!!！");
        }
        response.sendRedirect("showAllMusics");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doGet(request,response);
    }
}
